package microcredit.gui.table.impl;

import java.awt.Color;
import java.math.BigDecimal;

import javax.swing.ImageIcon;

import microcredit.gui.table.EnumBeanTableModel;
import microcredit.model.MemberToLoanEntry;

enum LoanEntryColumns {
	SEQUENCE, WEEK, AMOUNT, AMOUNT_PAID
//	, AMOUNT_FIXED;
}

public class LoanEntryTableModel extends EnumBeanTableModel<MemberToLoanEntry, LoanEntryColumns> {

	
	@Override
	protected Object getValue(MemberToLoanEntry o, LoanEntryColumns column) {
		switch (column) {
		case SEQUENCE : return o.getEntrySequenceID() + 1 
		+ (o.getSecurityPaid() != null && o.getSecurityPaid().booleanValue() ? "S" : "");
		case WEEK : return o.createDateString();
		case AMOUNT : return o.getAmount();
		case AMOUNT_PAID : return o.getAmountPaid();
//		case AMOUNT_FIXED : return o.getFixed();
		}
		return null;
	}
	
	@Override
	protected void setValue(MemberToLoanEntry o, Object value,
			LoanEntryColumns column) {
		switch (column) {
		case AMOUNT_PAID : 
			o.setAmountPaid((BigDecimal)value);
			fireUpdated(o, LoanEntryColumns.AMOUNT_PAID);
			break;
//		case AMOUNT_FIXED : 
//			o.setFixed((Boolean)value);
//			fireUpdated(o, LoanEntryColumns.AMOUNT_PAID, LoanEntryColumns.AMOUNT_FIXED);
//			break;
		}
	}
	
	@Override
	protected Class<?> getValueClass(LoanEntryColumns column) {
		switch (column) {
		case SEQUENCE : return Long.class;
		case WEEK : return String.class;
		case AMOUNT : return BigDecimal.class;
		case AMOUNT_PAID : return BigDecimal.class;
//		case AMOUNT_FIXED : return Boolean.class;
		}
		return String.class;
	}
	
	private static final Color ENTER_AMOUNT_COLOR = Color.decode("#b5f3b3");
	
	@Override
	protected Color getBackground(MemberToLoanEntry o, LoanEntryColumns column) {
		if (isLocked(o) || isFinalized(o)) {
			return Color.LIGHT_GRAY;
		}
		
		if (column == LoanEntryColumns.AMOUNT_PAID) {
			return ENTER_AMOUNT_COLOR;
		}
		return null;
	}
	
	private boolean isLocked(MemberToLoanEntry o) {
		if (o.getLocked() != null && o.getLocked().booleanValue()) {
			return true;
		}
		return false;
	}
	
	private boolean isFinalized(MemberToLoanEntry o) {
		if (o.getMemberToLoan() != null && o.getMemberToLoan().isFinalized()) {
			return true;
		}
		return false;
	}
	
	@Override
	protected ImageIcon getRowIcon(MemberToLoanEntry o) {
		if (isLocked(o) || isFinalized(o)) {
			return new ImageIcon(LoanEntryTableModel.class.getResource("/microcredit/icons/x16/lock.png"));
		} else {
			return new ImageIcon(LoanEntryTableModel.class.getResource("/microcredit/icons/x16/lock_open.png"));
		}
	}
	
	@Override
	protected boolean isEditable(MemberToLoanEntry o, LoanEntryColumns column) {
		if (column == LoanEntryColumns.AMOUNT_PAID) {
			return !isLocked(o);
		} 
		return false;
	}
	@Override
	protected String getName(LoanEntryColumns column) {
		switch (column) {
		case SEQUENCE : return "R";
		case WEEK : return "Calendar Week - Range";
		case AMOUNT : return "Amount";
		case AMOUNT_PAID : return "Paid";
//		case AMOUNT_FIXED : return "Finalized";
		}
		return null;
	}
	
	@Override
	protected int getWidth(LoanEntryColumns column) {
		switch (column) {
		case SEQUENCE : return 30;
		case WEEK : return 240;
		case AMOUNT : return 80;
		case AMOUNT_PAID : return 80;
		}
		return super.getWidth(column);
	}

}
